<template>
  <!--  <el-dialog v-model="visible" :close-on-click-modal="false" destroy-on-close width="60%" @close="closeMatter" title="沟通与联系">-->
  <communication-form ref="form" :code="code" :relateItem="relateItem" :enterpriseUsers="enterpriseUsers" :supplierId="supplierId"></communication-form>
  <el-row justify="end">
    <el-button size="small" @click="closeMatter">关闭</el-button>
    <el-button size="small" type="primary" @click="submitData">提交</el-button>
  </el-row>
  <!--    <template #footer>-->
  <!--        <span class="dialog-footer">-->
  <!--          <el-button size="small" @click="closeMatter">关闭</el-button>-->
  <!--          <el-button size="small" type="primary" @click="submitData">提交</el-button>-->
  <!--        </span>-->
  <!--    </template>-->
  <!--  </el-dialog>-->
</template>

<script>
// 沟通与联系 COMMUNICATION
import {computed, reactive, toRefs, ref} from 'vue'
// import Cookies from 'js-cookie'
// import Editor from '/@/components/editor/index.vue'
// import ModuleRelate from '/@/views/matter/components/ModuleRelate.vue'
// import Document from '/@/components/document/index.vue'
// import {ElMessage} from 'element-plus'
// import {Refresh} from '@element-plus/icons-vue'
// import {formatDate} from '/@/utils/formatTime'
// import {createMatterJust} from '/@/api/matter'
// import api from '/@/api/commonApi'
import CommunicationForm from '/@/views/matter/components/CommunicationForm.vue'

export default {
  name: 'Communication',
  emits: ['update:code'],
  components: {
    CommunicationForm
  },
  props: {
    code: {
      type: String,
      default: ''
    },
    relateItem: {
      type: Object,
      default: null
    },
    enterpriseUsers: {
      type: Array,
      default: []
    },
    supplierId: {
      type: String,
      default: {
        id: null
      }
    }
  },
  setup(props, context) {
    // const formRef = ref()
    // const {code, relateItem, enterpriseUsers,supplierId} = toRefs(props)
    const form = ref()
    // let visible = computed({
    //   get: () => {
    //     let flag = code.value == 'COMMUNICATION'
    //     // if (flag) {
    //     //   //预置一级关联
    //     //   state.presetRelate.code = relateItem.value.code
    //     //   state.presetRelate.dataId = supplierId.value
    //     //   // 默认带入自己
    //     //   state.selfId = Cookies.get('userId')
    //     //   state.formData.executorIds = [state.selfId]
    //     //   // 行动时间默认现在
    //     //   let nowTime = formatDate(new Date(), 'YYYY-mm-dd HH:MM:SS')
    //     //   state.formData.actionStartTime = nowTime
    //     //   state.dateTime = [nowTime, nowTime]
    //     //   if (relateItem.value) {   // 关联项带入
    //     //     state.formData.relateItems = [relateItem.value]
    //     //   }
    //     // }
    //     return flag
    //   },
    //   set: val => context.emit('update:code', val ? 'COMMUNICATION' : '')
    // })
    // const state = reactive({
    //   formData: {
    //     title: '沟通与联系',
    //     content: '',
    //     executorIds: [],
    //     relateItems: [],
    //     actionStartTime: null,
    //     actionEndTime: null,
    //     contactUserId: null,
    //     attach: '',
    //     attachNames: '',
    //     remark: null,
    //     type: 'COMMUNICATION'   // 事项类型编码
    //   },
    //   formRules: {
    //     title: {required: true, message: '请填写事项主题', trigger: 'blur'}
    //   },
    //   dateType: false,  // 行动时间范围/单体切换
    //   attach: [],
    //   dateTime: [],
    //   selfId: null,
    //   presetRelate:{
    //     code: null,
    //     dataId: null   //供应商ID
    //   }
    // })

    // const selectTime = (val) => {
    //   if (val && val.length === 2) {
    //     state.formData.actionStartTime = val[0]
    //     state.formData.actionEndTime = val[1]
    //   } else {
    //     state.formData.actionStartTime = state.formData.actionEndTime = null
    //   }
    // }
    // const executorChange = (val) => {
    //   if (!val.includes(state.selfId)) {
    //     state.formData.executorIds.push(state.selfId)
    //     ElMessage.warning('本人必须在执行人中，无法移除')
    //   }
    // }
    // const relateChange = (val) => {
    //   if (val && val.length > 0) {
    //     for (let i = 0; i < val.length; i++) {
    //       let item = val[i]
    //       if (item.label.substring(0, 2) === '客户') {
    //         // TODO 获取客户联系人
    //       }
    //     }
    //   }
    // }
    /**
     * 提交事项
     */
    const submitData = () => {
      form.value.submit()
      // formEl.validate(valid => {
      //   if (!valid) {
      //     return false
      //   }
      //   let params = Object.assign({}, state.formData)
      //   // 列表转逗号分割字符串提交
      //   if (state.attach.length > 0) {  // 组装附件地址和名称
      //     for (let i = 0; i < state.attach.length; i++) {
      //       let item = state.attach[i]
      //       params.attach += item.src
      //       params.attachNames += item.name
      //       if (i < state.attach.length - 1) {
      //         params.attach += ','
      //         params.attachNames += '，'
      //       }
      //     }
      //   }
      //   createMatterJust(params).then(res => {
      //     if (res.data) {
      //       ElMessage.success('提交成功！')
      //       // 关联事项主键
      //       let data = state.attach.map(item => {
      //         item.matterId = res.data.agendaMatterId
      //         item.matterName = res.data.title
      //       })
      //       api.fileRelevanceModule(data)
      //       closeMatter()
      //     }
      //   }).catch(e => console.log(e))
      // })
    }
    const closeMatter = () => {
      form.value.reset()
      context.emit('update:code', '')
    }

    return {
      form,
      // ...toRefs(state),
      // supplierId,
      // selectTime,
      // executorChange,
      submitData,
      // relateChange,
      // Refresh,
      // visible,
      closeMatter
      // enterpriseUsers
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
